<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Admin extends Admin_Controller
{
	/**
	 * Constructor method
	 */
	public function __construct()
	{
		parent::__construct();

		$this->load->helper('users/user');
	}

	/**
	 * Show the control panel
	 */
	public function index()
	{
		$this->template
			//->enable_parser(true)
			->title('Admin');
		$this->template
			->build('admin/index');//dashboard
	}

	/**
	 * Log in
	 */
 	function lang( $code = NULL ){
		//echo $code;exit;
		$this->load->config('language');
		$this->lang_array = $this->config->item('supported_languages');
		$this->session->set_userdata('ss_lang',$this->lang_array[$code]);
		$this->_lang = $code;
//		echo $code;
		redirect('admin');
	}
	public function login()
	{
		$this->load->model('users/user_m');
		// Set the validation rules
		$this->validation_rules = array(
			array(
				'field' => 'username',
				'label' => lang('global:email'),
				'rules' => 'required'//callback__check_login
			),
			array(
				'field' => 'password',
				'label' => lang('global:password'),
				'rules' => 'required'
			)
		);

		// Call validation and set rules
//		$this->load->library('form_validation');
		$this->form_validation->set_rules($this->validation_rules);

		$type =  substr($this->input->post('submit'), 0);
		if($type){
			if ($this->form_validation->run())//or $this->ion_auth->logged_in()
			{
				$model = $this->input->post();
				$this->current_user = $this->user_m->login($model['username'],$model['password']);
				//luu thong tin $this->profile
				
				if($this->current_user){
					$this->load->model('emp_profile/profile_m');
					$this->current_profile = $this->profile_m->get($this->current_user->id);
					
					$redirect = $this->session->userdata('admin_redirect');
					$this->session->set_userdata('current_user', $this->current_user);
					$this->session->set_userdata('current_profile', $this->current_profile);
					//$this->session->unset_userdata('admin_redirect');
					redirect(!empty($redirect) ? $redirect : 'admin','refresh');
				}
			}
		}
		
		$this->template
			->set_layout(false)
			->build('admin/login');
	}

	/**
	 * Logout
	 */
	public function logout()
	{
		$this->session->sess_destroy();
		redirect('admin/login','refresh');
	}

	/**
	 * Callback From: login()
	 *
	 * @param string $email The Email address to validate
	 *
	 * @return bool
	 */
	public function _check_login($email)
	{
		if ($this->ion_auth->login($email, $this->input->post('password'), (bool)$this->input->post('remember')))
		{
			Events::trigger('post_admin_login');
			
			return true;
		}

		Events::trigger('login_failed', $email);
		error_log('Login failed for user '.$email);

		$this->form_validation->set_message('_check_login', $this->ion_auth->errors());
		return false;
	}

	/**
	 * Display the help string from a module's
	 * details.php file in a modal window
	 *
	 * @param	string	$slug	The module to fetch help for
	 *
	 * @return	void
	 */
	public function help($slug)
	{
		$this->template
			->set_layout('modal', 'admin')
			->set('help', $this->module_m->help($slug))
			->build('admin/partials/help');
	}

	public function remove_installer_directory()
	{
		if ( ! $this->input->is_ajax_request())
		{
			die('Nope, sorry');
		}

		header('Content-Type: application/json');

		if (is_dir('./installer'))
		{
			$this->load->helper('file');
			// if the contents of "installer" delete successfully then finish off the installer dir
			if (delete_files('./installer', true) and count(scandir('./installer')) == 2)
			{
				rmdir('./installer');
				// This is the end, tell Sally I loved her.
				die(json_encode(array('status' => 'success', 'message' => lang('cp:delete_installer_successfully_message'))));
			}
		}

		die(json_encode(array('status' => 'error', 'message' => lang('cp:delete_installer_manually_message'))));
	}
	
	function form(){
		$model = $this->input->post();
	
		//validate
		$this->form_validation->set_rules('title', 'Tên', 'required');
		//end validate
		
		if ( substr($this->input->post('btnAction'), 0, 4) == 'save' ) {
			if ($this->form_validation->run() == FALSE)
			{
				//$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
				//$data['errors'] = validation_errors();
				//$this->form_validation->set_errors(array('title' => "'%s' is required."));
			}
			else{
				echo "ok";exit;
			}
		}
		$this->template
			->set('model',$model)
			->build($this->controller.'/form');
	}
	
	function detect(){
		//echo 1;exit;
			$this->template->set_layout('detect.html')->build('detect.html');
	}
}